package com.github.binarywang.demo.wx.miniapp.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.github.binarywang.demo.wx.miniapp.config.WxMaConfiguration;
import com.github.binarywang.demo.wx.miniapp.config.enums.AppletStatusEnum;
import com.github.binarywang.demo.wx.miniapp.domain.ResponseEntity;
import com.github.binarywang.demo.wx.miniapp.domain.SendMessageBody;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 智慧实验室
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxmini/smart/laboratory")
@Slf4j
public class WxSmartLaboratoryController {

    //获取openId
    @GetMapping("/wxcallback")
    public String wxcallback(String code) throws IOException {
        WxMaService wxMaService = WxMaConfiguration.maServices.get(AppletStatusEnum.SMART_LABORATORY.getAppid());
        WxMaJscode2SessionResult sessionResult =null;
        try {
            sessionResult = wxMaService.jsCode2SessionInfo(code);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        System.out.println(sessionResult.getOpenid());
        return sessionResult.getOpenid();
    }

    /*{
        "openId":"oXnuW5aBjru1Flox3tDi_cnHXwPI",
        "messageType":"智慧实验室",
        "sendContent":"lalalal",
        "sendingTime":"2024-05"
    }*/

    //发送通知
    @PostMapping("/send")
    public ResponseEntity SendSubscribe(SendMessageBody sendMessageBody) throws WxErrorException {
        WxMaService wxMaService = WxMaConfiguration.maServices.get(AppletStatusEnum.SMART_LABORATORY.getAppid());
        List<WxMaSubscribeMessage.MsgData> list = new ArrayList<>();
        //消息类型
        WxMaSubscribeMessage.MsgData messageType = new WxMaSubscribeMessage.MsgData();
        messageType.setName("phrase4");
        messageType.setValue(sendMessageBody.getMessageType());
        list.add(messageType);
        //发送内容
        WxMaSubscribeMessage.MsgData sendContent = new WxMaSubscribeMessage.MsgData();
        sendContent.setName("thing2");
        sendContent.setValue(sendMessageBody.getSendContent());
        list.add(sendContent);
        //发送时间
        WxMaSubscribeMessage.MsgData sendingTime = new WxMaSubscribeMessage.MsgData();
        sendingTime.setName("time3");
        sendingTime.setValue(sendMessageBody.getSendingTime());
        list.add(sendingTime);

        WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
            .toUser(sendMessageBody.getOpenId())
            .templateId(AppletStatusEnum.SMART_LABORATORY.getCode())
            .page("")
            .data(list)
            .build();
        wxMaService.getSubscribeService().sendSubscribeMsg(wxMaSubscribeMessage);

        ResponseEntity entity = new ResponseEntity();
        entity.setCode("200");
        entity.setMessage("发送成功");
        return entity;
    }


}
